﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.IO;
using System.Drawing.Imaging;
using System.Drawing;
using AjaxControlToolkit;
using System.Net;
using System.Net.Mail;
using ASPDotNetSamples;


public partial class Front_ReferFriend : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
        try {
            setSecureProtocol(false);
            if (!IsPostBack) {
                if (Membership.GetUser() != null)
                    Session["username"] = Membership.GetUser().UserName.ToString();
                if (Session["username"] == null) {
                    referFriend.Visible = true;
                    referLoginFriend.Visible = false;
                } else {
                    string CityID = "";
                    if (Request.Cookies["CityID"] != null) {
                        CityID = Request.Cookies["CityID"].Value;
                    }
                    referFriend.Visible = false;
                    referLoginFriend.Visible = true;
                    string UserID = Membership.GetUser().ProviderUserKey.ToString();
                    var sub = new DealesDataContext();
                    var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(CityID));
                    int DealID = 0;
                    foreach (var item in query1) {
                        DealID = Convert.ToInt32(item.DealID);
                    }

                    //string referUrl = Util.GetShortUrl("http://www.zupons.com/Default.aspx?ReferUserID=" + UserID + "&CityID=" + CityID.ToString());
                    //string referUrl = Util.RewriteUrl("http://www.zupons.com/Default.aspx?ReferUserID=" + UserID + "&CityID=" + CityID.ToString());
                    string referUrl = "http://www.zupons.com/Default.aspx?ReferUserID=" + UserID + "&CityID=" + CityID.ToString();

                    textLink.Text = referUrl;
                    Twiteer.HRef = "http://twitter.com/home?status=https://www.zupons.com/Default.aspx?dealID=" + DealID.ToString();

                    FaceBook.HRef = "http://www.facebook.com/sharer.php?u=https://www.zupons.com/Default.aspx?dealID=" + DealID.ToString();
                    Twiteer1.HRef = "http://twitter.com/home?status=https://www.zupons.com/Default.aspx?dealID=" + DealID.ToString();

                    FaceBook1.HRef = "http://www.facebook.com/sharer.php?u=https://www.zupons.com/Default.aspx?dealID=" + DealID.ToString();

                    sendReferEmail.HRef =
                        "mailto:?body=Check out Zupons's daily deal - huge discounts on the coolest stuff in your city. " +
                        referUrl + "&subject=I think you should get your Zupons";
                }
            }
        } catch {
        }
    }
    public void setSecureProtocol(bool bSecure) {
        string redirectUrl = null;

        // if we want HTTPS and it is currently HTTP
        if (bSecure && !Request.IsSecureConnection)
            redirectUrl = Request.Url.ToString().Replace("http:", "https:");

        else

            // if we want HTTP and it is currently HTTPS
            if (!bSecure && Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("https:", "http:");

        //else

        // in all other cases we don't need to redirect

        // check if we need to redirect, and if so use redirectUrl to do the job
        if (redirectUrl != null)

            Response.Redirect(redirectUrl);

    }
    protected void lnkBtnMailIt_Click(object sender, EventArgs e) {
        To.Visible = true;
        From.Visible = true;
        Send.Visible = true;
        txtTo.Text = "";
        txtFrom.Text = "";
        txtErrorMsg.Text = "";
    }
    protected void btnReferSend_Click(object sender, EventArgs e) {
        try {

            string Url = "<a href=" + textLink.Text + ">" + textLink.Text + "</a>";
            //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
            //replacements.Add("<%From%>", txtFrom.Text);
            //replacements.Add("<%MailSubject%>", Url);
            //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl, Mailer.MessageTemplate.ReferLink, replacements, txtTo.Text.ToString(), "Refer Friends");
            //Mailer.InternalSend(confirmation, false);
            MailMessage Mail = new MailMessage();
            MailAddress ma = new MailAddress("mail@zupons.com", "Zupons");
            Mail.From = ma;
            Mail.To.Add(txtTo.Text.ToString());
            Mail.Subject = "Refer Friends";
            Mail.Body = Url;
            Mail.IsBodyHtml = true;
            try {
                SmtpClient smtpMailObj = new SmtpClient();
                smtpMailObj.Host = "mail.zupons.com";
                smtpMailObj.Send(Mail);
                // Response.Write("Your Message has been sent successfully");
            } catch (Exception ex) {
                //Response.Write("Message Delivery Fails");
            }
            txtErrorMsg.Visible = true;
            txtErrorMsg.Text = "Message Sent successfully.";
            txtTo.Text = "";
            txtFrom.Text = "";
        } catch (Exception ex) {
            txtErrorMsg.Visible = true;
            txtErrorMsg.Text = ex.Message;
        }
    }
    protected void Image3_Click(object sender, ImageClickEventArgs e) {
        To.Visible = true;
        From.Visible = true;
        Send.Visible = true;
        txtTo.Text = "";
        txtFrom.Text = "";
        txtErrorMsg.Text = "";
    }
    protected void BtnCancel_Click(object sender, EventArgs e) {
        Response.Redirect("referfriend.aspx");
    }

}
